using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.UI.Composition;
using Microsoft.UI.Composition.Imaging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.System;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace App1
{
    /// <summary>
    /// An empty window that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainWindow : Window
    {
        private bool _isSidebarExpanded = true;
        private double _sidebarWidth = 240;
        private double _sidebarCollapsedWidth = 48;

        public MainWindow()
        {
            InitializeComponent();
            InitializeNavigation();
            ApplySystemTheme();
        }

        private void InitializeNavigation()
        {
            // 注册主题更改事件
            SystemThemeHelper.ThemeChanged += OnThemeChanged;

            // 初始导航到文件共享页面
            MainContentFrame.Navigate(typeof(FileSharePage));

            // 设置侧边栏初始宽度
            Sidebar.Width = _sidebarWidth;
        }

        private void ApplySystemTheme()
        {
            // 应用系统主题
            var isDarkMode = SystemThemeHelper.IsDarkMode();
            RequestedTheme = isDarkMode ? ElementTheme.Dark : ElementTheme.Light;
        }

        private void OnThemeChanged(bool isDarkMode)
        {
            // 主题更改时更新UI
            RequestedTheme = isDarkMode ? ElementTheme.Dark : ElementTheme.Light;
        }

        // 窗口控制按钮事件
        private void MinimizeButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void MaximizeButton_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = this.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        // 侧边栏折叠/展开按钮事件
        private void ToggleSidebarButton_Click(object sender, RoutedEventArgs e)
        {
            _isSidebarExpanded = !_isSidebarExpanded;

            // 动画过渡
            Sidebar.Width = _isSidebarExpanded ? _sidebarWidth : _sidebarCollapsedWidth;

            // 显示或隐藏文本
            HomeText.Visibility = _isSidebarExpanded ? Visibility.Visible : Visibility.Collapsed;
            DownloadsText.Visibility = _isSidebarExpanded ? Visibility.Visible : Visibility.Collapsed;
            SettingsText.Visibility = _isSidebarExpanded ? Visibility.Visible : Visibility.Collapsed;
        }

        // 侧边栏菜单项点击事件
        private void HomeButton_Click(object sender, RoutedEventArgs e)
        {
            MainContentFrame.Navigate(typeof(FileSharePage));
        }

        private void DownloadsButton_Click(object sender, RoutedEventArgs e)
        {
            MainContentFrame.Navigate(typeof(DownloadsPage));
        }

        private void SettingsButton_Click(object sender, RoutedEventArgs e)
        {
            MainContentFrame.Navigate(typeof(SettingsPage));
        }
    }

    // 系统主题辅助类
    public static class SystemThemeHelper
    {
        public static event Action<bool> ThemeChanged;

        public static bool IsDarkMode()
        {
            var uiSettings = new Windows.UI.ViewManagement.UISettings();
            var color = uiSettings.GetColorValue(Windows.UI.ViewManagement.UIColorType.Background);
            return color.R < 128 && color.G < 128 && color.B < 128;
        }
    }
}
